package com.qianhui.bean;

import com.qianhui.common.CM;
import com.qianhui.common.IStatus;
import com.qianhui.dao.*;
import com.qianhui.pojo.Buyer;
import com.qianhui.pojo.CommissionMonthRecord;
import com.qianhui.pojo.PreCommissionMonthRecord;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class BatchTaskJob_OneMonth implements Job
{

    private GoodsDAO goodsDAO = new GoodsDAO();
    private OrderDAO orderDAO = new OrderDAO();
    private OrderGoodsDAO orderGoodsDAO = new OrderGoodsDAO();
    private CommentDAO commentDAO = new CommentDAO();
    private BuyerDAO buyerDAO = new BuyerDAO();
    CommissionDayRecordDAO commissionDayRecordDAO = new CommissionDayRecordDAO();
    PreCommissionDayRecordDAO preCommissionDayRecordDAO = new PreCommissionDayRecordDAO();
    CommissionMonthRecordDAO commissionMonthRecordDAO = new CommissionMonthRecordDAO();
    PreCommissionMonthRecordDAO preCommissionMonthRecordDAO = new PreCommissionMonthRecordDAO();
    GoodsCommissionDAO goodsCommissionDAO = new GoodsCommissionDAO();

    @Override
    public void execute(JobExecutionContext arg0) throws JobExecutionException
    {
        System.out.println(CM.getTimeStrNow() + "每月1日上午5:00的批量任务执行--开始");
        batch_count_commission_month_record();
        batch_count_pre_commission_month_record();
        System.out.println(CM.getTimeStrNow() + "每月1日上午5:00的批量任务执行--结束");
    }

    private void batch_count_commission_month_record()
    {
        System.out.println("批量计算每月佣金--开始");

        List<String> typeList = new ArrayList<String>();
        typeList.add(IStatus.BUYER_TYPE_1);
        typeList.add(IStatus.BUYER_TYPE_2);
        typeList.add(IStatus.BUYER_TYPE_3);
        typeList.add(IStatus.BUYER_TYPE_4);
        typeList.add(IStatus.BUYER_TYPE_5);
        typeList.add(IStatus.BUYER_TYPE_6);
        typeList.add(IStatus.BUYER_TYPE_7);
        typeList.add(IStatus.BUYER_TYPE_8);
        typeList.add(IStatus.BUYER_TYPE_9);
        typeList.add(IStatus.BUYER_TYPE_10);
        typeList.add(IStatus.BUYER_TYPE_11);

        // 找到需要计算佣金的人员
        int need_count_buyer_number = buyerDAO.getTotalCountByStatusAndTypeList_keyword(IStatus.BUYER_STATUS_USED, typeList, null);
        int onePageSize_2 = 2;
        int onePageSize_5 = 5;
        int onePageSize_10 = 10;

        Calendar cald = Calendar.getInstance();

        // 得到本月1号0时0分0秒0毫秒
        cald.set(Calendar.DAY_OF_MONTH, 1);
        cald.set(Calendar.HOUR_OF_DAY, 0);
        cald.set(Calendar.MINUTE, 0);
        cald.set(Calendar.SECOND, 0);
        cald.set(Calendar.MILLISECOND, 0);
        // 减去1毫秒,得到上个月最后一天23时59分59秒999毫秒
        cald.add(Calendar.MILLISECOND, -1);
        Date month_endTime = cald.getTime();

        // 上个月第一天0时0分0秒0毫秒
        cald.set(Calendar.DAY_OF_MONTH, 1);
        cald.set(Calendar.HOUR_OF_DAY, 0);
        cald.set(Calendar.MINUTE, 0);
        cald.set(Calendar.SECOND, 0);
        cald.set(Calendar.MILLISECOND, 0);
        // 当月第一天0时0分0秒0毫秒
        Date month_beginTime = cald.getTime();

        Calendar recordMonth_cal = Calendar.getInstance();
        recordMonth_cal.setTime(month_endTime);
        recordMonth_cal.set(Calendar.HOUR_OF_DAY, 0);
        recordMonth_cal.set(Calendar.MINUTE, 0);
        recordMonth_cal.set(Calendar.SECOND, 0);
        recordMonth_cal.set(Calendar.MILLISECOND, 0);
        Date recordMonth = recordMonth_cal.getTime();

        if (need_count_buyer_number > 0)
        {
            int ttpTemp = need_count_buyer_number % onePageSize_2 == 0 ? need_count_buyer_number / onePageSize_2 : need_count_buyer_number / onePageSize_2 + 1;
            for (int i = 1; i <= ttpTemp; i++)
            {
                List<Buyer> buyerList = buyerDAO.getListByIndexAndPageSizeAndStatusAndTypeList_keyword(i, onePageSize_2, IStatus.BUYER_STATUS_USED, typeList, null);
                if (buyerList != null && buyerList.size() > 0)
                {
                    for (Buyer buyerTemp : buyerList)
                    {
                        // 找到此人的所有下线买家
                        int buyer_number_of_this_introducer = buyerDAO.getTotalCountByBuyerIntroducer(buyerTemp.getBuyerId());
                        if (buyer_number_of_this_introducer == 0)
                        {
                            CommissionMonthRecord monthRecord = new CommissionMonthRecord();
                            monthRecord.setRecordId(CM.getRandomTwentyFivePrimaryKeyId());
                            monthRecord.setBuyerId(buyerTemp.getBuyerId());
                            monthRecord.setTotalBuyerNumber(0);
                            monthRecord.setMonthOrderNumber(0);
                            monthRecord.setMonthGoodsNumber(0);
                            monthRecord.setMonthCommission(0.0);
                            monthRecord.setRecordMonth(recordMonth);

                            CommissionMonthRecord monthRecordFromDB = commissionMonthRecordDAO.findByBuyerIdAndRecordMonth(buyerTemp.getBuyerId(), recordMonth);
                            if (monthRecordFromDB == null)
                            {
                                commissionMonthRecordDAO.save(monthRecord);
                            }
                        }
                        else if (buyer_number_of_this_introducer > 0)
                        {
                            int totalBuyerNumber = buyer_number_of_this_introducer;
                            int monthOrderNumber = 0;
                            int monthGoodsNumber = 0;
                            double monthCommission = 0;

                            monthOrderNumber = commissionDayRecordDAO.getTotalOrderNumberByBuyerIdAndBeginTimeAndEndTime(buyerTemp.getBuyerId(), month_beginTime, month_endTime);
                            monthGoodsNumber = commissionDayRecordDAO.getTotalGoodsNumberByBuyerIdAndBeginTimeAndEndTime(buyerTemp.getBuyerId(), month_beginTime, month_endTime);
                            monthCommission = commissionDayRecordDAO.getTotalCommissionByBuyerIdAndBeginTimeAndEndTime(buyerTemp.getBuyerId(), month_beginTime, month_endTime);

                            if (monthOrderNumber >= 0 && monthGoodsNumber >= 0 && monthCommission >= 0)
                            {
                                CommissionMonthRecord monthRecord = new CommissionMonthRecord();
                                monthRecord.setRecordId(CM.getRandomTwentyFivePrimaryKeyId());
                                monthRecord.setBuyerId(buyerTemp.getBuyerId());
                                monthRecord.setTotalBuyerNumber(totalBuyerNumber);
                                monthRecord.setMonthOrderNumber(monthOrderNumber);
                                monthRecord.setMonthGoodsNumber(monthGoodsNumber);
                                monthRecord.setMonthCommission(monthCommission);
                                monthRecord.setRecordMonth(recordMonth);
                                CommissionMonthRecord monthRecordFromDB = commissionMonthRecordDAO.findByBuyerIdAndRecordMonth(buyerTemp.getBuyerId(), recordMonth);
                                if (monthRecordFromDB == null)
                                {
                                    commissionMonthRecordDAO.save(monthRecord);
                                }
                            }
                        }
                    }
                }
            }
        }

        System.out.println("批量计算每月佣金--结束");
    }

    private void batch_count_pre_commission_month_record()
    {
        System.out.println("批量计算预测每月佣金--开始");

        List<String> typeList = new ArrayList<String>();
        typeList.add(IStatus.BUYER_TYPE_1);
        typeList.add(IStatus.BUYER_TYPE_2);
        typeList.add(IStatus.BUYER_TYPE_3);
        typeList.add(IStatus.BUYER_TYPE_4);
        typeList.add(IStatus.BUYER_TYPE_5);
        typeList.add(IStatus.BUYER_TYPE_6);
        typeList.add(IStatus.BUYER_TYPE_7);
        typeList.add(IStatus.BUYER_TYPE_8);
        typeList.add(IStatus.BUYER_TYPE_9);
        typeList.add(IStatus.BUYER_TYPE_10);
        typeList.add(IStatus.BUYER_TYPE_11);

        // 找到需要计算佣金的人员
        int need_count_buyer_number = buyerDAO.getTotalCountByStatusAndTypeList_keyword(IStatus.BUYER_STATUS_USED, typeList, null);
        int onePageSize_2 = 2;
        int onePageSize_5 = 5;
        int onePageSize_10 = 10;

        Calendar cald = Calendar.getInstance();

        // 得到本月1号0时0分0秒0毫秒
        cald.set(Calendar.DAY_OF_MONTH, 1);
        cald.set(Calendar.HOUR_OF_DAY, 0);
        cald.set(Calendar.MINUTE, 0);
        cald.set(Calendar.SECOND, 0);
        cald.set(Calendar.MILLISECOND, 0);
        // 减去1毫秒,得到上个月最后一天23时59分59秒999毫秒
        cald.add(Calendar.MILLISECOND, -1);
        Date month_endTime = cald.getTime();

        // 上个月第一天0时0分0秒0毫秒
        cald.set(Calendar.DAY_OF_MONTH, 1);
        cald.set(Calendar.HOUR_OF_DAY, 0);
        cald.set(Calendar.MINUTE, 0);
        cald.set(Calendar.SECOND, 0);
        cald.set(Calendar.MILLISECOND, 0);
        // 当月第一天0时0分0秒0毫秒
        Date month_beginTime = cald.getTime();

        Calendar recordMonth_cal = Calendar.getInstance();
        recordMonth_cal.setTime(month_endTime);
        recordMonth_cal.set(Calendar.HOUR_OF_DAY, 0);
        recordMonth_cal.set(Calendar.MINUTE, 0);
        recordMonth_cal.set(Calendar.SECOND, 0);
        recordMonth_cal.set(Calendar.MILLISECOND, 0);
        Date recordMonth = recordMonth_cal.getTime();

        if (need_count_buyer_number > 0)
        {
            int ttpTemp = need_count_buyer_number % onePageSize_2 == 0 ? need_count_buyer_number / onePageSize_2 : need_count_buyer_number / onePageSize_2 + 1;
            for (int i = 1; i <= ttpTemp; i++)
            {
                List<Buyer> buyerList = buyerDAO.getListByIndexAndPageSizeAndStatusAndTypeList_keyword(i, onePageSize_2, IStatus.BUYER_STATUS_USED, typeList, null);
                if (buyerList != null && buyerList.size() > 0)
                {
                    for (Buyer buyerTemp : buyerList)
                    {
                        // 找到此人的所有下线买家
                        int buyer_number_of_this_introducer = buyerDAO.getTotalCountByBuyerIntroducer(buyerTemp.getBuyerId());
                        if (buyer_number_of_this_introducer == 0)
                        {
                            PreCommissionMonthRecord monthRecord = new PreCommissionMonthRecord();
                            monthRecord.setRecordId(CM.getRandomTwentyFivePrimaryKeyId());
                            monthRecord.setBuyerId(buyerTemp.getBuyerId());
                            monthRecord.setTotalBuyerNumber(0);
                            monthRecord.setMonthOrderNumber(0);
                            monthRecord.setMonthGoodsNumber(0);
                            monthRecord.setMonthCommission(0.0);
                            monthRecord.setRecordMonth(recordMonth);

                            PreCommissionMonthRecord monthRecordFromDB = preCommissionMonthRecordDAO.findByBuyerIdAndRecordMonth(buyerTemp.getBuyerId(), recordMonth);
                            if (monthRecordFromDB == null)
                            {
                                preCommissionMonthRecordDAO.save(monthRecord);
                            }
                        }
                        else if (buyer_number_of_this_introducer > 0)
                        {
                            int totalBuyerNumber = buyer_number_of_this_introducer;
                            int monthOrderNumber = 0;
                            int monthGoodsNumber = 0;
                            double monthCommission = 0;

                            monthOrderNumber = preCommissionDayRecordDAO.getTotalOrderNumberByBuyerIdAndBeginTimeAndEndTime(buyerTemp.getBuyerId(), month_beginTime, month_endTime);
                            monthGoodsNumber = preCommissionDayRecordDAO.getTotalGoodsNumberByBuyerIdAndBeginTimeAndEndTime(buyerTemp.getBuyerId(), month_beginTime, month_endTime);
                            monthCommission = preCommissionDayRecordDAO.getTotalCommissionByBuyerIdAndBeginTimeAndEndTime(buyerTemp.getBuyerId(), month_beginTime, month_endTime);

                            if (monthOrderNumber >= 0 && monthGoodsNumber >= 0 && monthCommission >= 0)
                            {
                                PreCommissionMonthRecord monthRecord = new PreCommissionMonthRecord();
                                monthRecord.setRecordId(CM.getRandomTwentyFivePrimaryKeyId());
                                monthRecord.setBuyerId(buyerTemp.getBuyerId());
                                monthRecord.setTotalBuyerNumber(totalBuyerNumber);
                                monthRecord.setMonthOrderNumber(monthOrderNumber);
                                monthRecord.setMonthGoodsNumber(monthGoodsNumber);
                                monthRecord.setMonthCommission(monthCommission);
                                monthRecord.setRecordMonth(recordMonth);
                                PreCommissionMonthRecord monthRecordFromDB = preCommissionMonthRecordDAO.findByBuyerIdAndRecordMonth(buyerTemp.getBuyerId(), recordMonth);
                                if (monthRecordFromDB == null)
                                {
                                    preCommissionMonthRecordDAO.save(monthRecord);
                                }
                            }
                        }
                    }
                }
            }
        }

        System.out.println("批量计算预测每月佣金--结束");
    }
}
